/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;

import pcgen.android.Logger;
import pcgen.android.app.ParcelableEx;

import pcgen.CharacterViewer.utility.IResource;

public abstract class GameResourceParcelable extends ParcelableEx implements IResource
{
	public GameResourceParcelable()
	{
	}
	
	public GameResourceParcelable(Parcel in)
	{
		super(in);
	}
	
	protected <T extends ArrayList<E>, E extends IResource> T loadArray(T list, JSONObject data, String name, Class<E> classType)
	{
		try
    	{
			JSONArray listJSON = data.getJSONArray(name);
			for (int index = 0; index < listJSON.length(); index++)
			{
				if (listJSON.isNull(index))
					continue;
				
				JSONObject itemJSON = listJSON.getJSONObject(index);
				
				//E item = classType.newInstance();
				//item.load(itemJSON);
				E item = initLoadItem(itemJSON, classType);
				
				list.add(item);
			}
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "loadArray", tr);
    	}
		
		return list;
	}
	
	protected <E extends IResource> E initLoadItem(JSONObject itemJSON, Class<E> classType) 
		throws Throwable
	{
		E item = classType.newInstance();
		item.load(itemJSON);
		return item;
	}
	
	public <E extends IResource> void saveArray(ArrayList<E> list, JSONObject data, String name)
	{
		try
    	{
			JSONArray listJSON = new JSONArray();
			for (E item : list)
			{
				JSONObject itemJSON = item.save();
				listJSON.put(itemJSON);
			}
			data.put("resources", listJSON);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "saveArray", tr);
    	}
	}
	
	protected boolean getJSONBoolean(JSONObject data, String name) 
		throws JSONException
	{
		if (data.has(name))
			return data.getBoolean(name);
		
		return false;
	}
	
	protected double getJSONDouble(JSONObject data, String name) 
		throws JSONException
	{
		if (data.has(name))
			return data.getDouble(name);
		
		return 0;
	}
	
	protected int getJSONInt(JSONObject data, String name) 
		throws JSONException
	{
		if (data.has(name))
			return data.getInt(name);
		
		return 0;
	}
	
	protected int getJSONInt(JSONObject data, String name, int defaultValue) 
		throws JSONException
	{
		if (data.has(name))
			return data.getInt(name);
		
		return defaultValue;
	}
	
	protected JSONObject getJSONObject(JSONObject data, String name) 
		throws JSONException
	{
		if (data.has(name))
			return data.getJSONObject(name);
		
		return null;
	}
	
	protected String getJSONString(JSONObject data, String name) 
		throws JSONException
	{
		if (data.has(name))
			return data.getString(name);
		
		return "";
	}

	private static final String TAG = GameResourceParcelable.class.getSimpleName();
}
